package com.service;

import com.mapper.MessageMapper;
import com.model.Message;
import com.model.MessageDTO;
import com.model.Zpinfo;
import com.util.PageBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MessageServiceImpl implements MessageService{

    @Resource
    private MessageMapper messageMapper;

    @Override
    public int addMessage(Message message) {
        message.setCdate(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        return messageMapper.insert(message);
    }

    @Override
    public int getCount(Message message) {
        return messageMapper.getCount(message);
    }

    @Override
    public List<MessageDTO> queryMessageByUid(Message message, PageBean page) {
        Map<String, Object> map = getQueryMap(message, page);
        List<MessageDTO> list = messageMapper.queryMessageByUid(map);

        return list;
    }

    @Override
    public List<Message> queryMessageListByFromId(Message message) {

        List<Message> list = messageMapper.queryMessageListByFromId(message);
        return list;
    }


    private Map<String, Object> getQueryMap(Message message, PageBean page) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (message != null) {
            map.put("toid", message.getToid());
        }
        if (page != null) {
            PageBean.setPageMap(map, page);
        }
        return map;
    }


}
